package org.linlinjava.litemall.core.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by 姜青林 on 2018/3/2 15:12
 */
public class MoneyUtil {
    public MoneyUtil() {
    }

    public static String fen2yuan(Long money) {
        return money == null ? "0" : (new BigDecimal(money)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
    }

    public static String fen2yuan(Integer money) {
        return money == null ? "0" : (new BigDecimal(money)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
    }

    public static Double fen2yuanD(Integer money) {
        return money == null ? 0.0D : (new BigDecimal(money)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).doubleValue();
    }

    public static BigDecimal fen2yuanBigDecimal(Integer money) {
        return money == null ? new BigDecimal(0) : (new BigDecimal(money)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
    }

    public static Integer yuan2fen(Double money) {
        if (money == null) {
            return 0;
        } else {
            try {
                String str = roundHalfUp(money);
                str = str.replace(".", "");
                return Math.abs(Integer.parseInt(str));
            } catch (Exception var2) {
                return 0;
            }
        }
    }

    public static Integer yuan2fen(String money) {
        return money != null && !"".equals(money.trim()) ? yuan2fen(Double.parseDouble(money)) : 0;
    }

    public static Integer yuan2fen(BigDecimal money) {
        if (money == null) {
            return 0;
        } else {
            money.setScale(2, RoundingMode.HALF_UP);
            return yuan2fen(money.doubleValue());
        }
    }

    public static Integer getPoints(BigDecimal points) {
        return points == null ? 0 : Math.abs(points.intValue());
    }

    public static Integer getPoints(Double points) {
        return points == null ? 0 : Math.abs(points.intValue());
    }

    public static Integer getPoints(String points) {
        try {
            return Math.abs(Integer.parseInt(points));
        } catch (Exception var2) {
            return 0;
        }
    }

    public static double roundHalfUpDouble(Double val) {
        if (val == null) {
            val = 0.0D;
        }

        BigDecimal valbd = (new BigDecimal(val)).setScale(2, RoundingMode.HALF_UP);
        return valbd.doubleValue();
    }

    public static String roundHalfUp(Double val) {
        if (val == null) {
            val = 0.0D;
        }

        BigDecimal valbd = (new BigDecimal(val)).setScale(2, RoundingMode.HALF_UP);
        return valbd.toString();
    }

    public static String fen2yuan2m(Integer money) {
        if(money == null || money == 0){
            return "0.0";
        }
        DecimalFormat df = new DecimalFormat("#0.0#");
        BigDecimal moneyB = (new BigDecimal(money)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
        return df.format(moneyB);
    }

    public static void main(String[] args) {
//        System.out.println(fen2yuan(100));
//        System.out.println(yuan2fen(0.0D));
//        System.out.println(Double.valueOf(100.10));
        System.out.println(fen2yuan2m(10));
        Integer a = 0;
        System.out.println(a.doubleValue());
    }
}
